<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="/admin/common/uploadImage"
      :show-file-list="false"
      :http-request="uploadimg"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
  </div>
</template>

<script>
import { imgAPI } from '@/api/Bioinformatics'
export default {
  props: {
    value: {
      type: String,
      default: () => ''
    }
  },
  data() {
    return {
      // 图片回显
      imageUrl: ''

    }
  },
  watch: {
    value: {
      handler() {
        this.imageUrl = this.value
      },
      immediate: true
    }
  },
  methods: {
    // // 图片上传
    beforeAvatarUpload(file) {
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return isLt5M
    },
    async uploadimg({ file }) {
      try {
        const res = await imgAPI(file)
        this.imageUrl = `http://forest.kmdsoft.net/storage/${res}`
        this.$emit('input', this.imageUrl)
        console.log(this.imageUrl)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
// 图片样式
.avatar-uploader-icon {
  border: 1px dashed #333;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #333;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
·
